<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\Carousel;
use App\Models\Product;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class CarouselController extends AdminController
{
    public function getProduct(): array
    {
        $res = Product::query()->where('status', 1)->pluck('name', 'id')->toArray();
        $res[0] = '无';
        return $res;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Carousel(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('product_id')->using($this->getProduct());
            $grid->column('image_path')->image('', 100, 100);
            $grid->column('title');
            $grid->column('description');
            $grid->column('link')->link();
            $grid->column('order')->editable();
            $grid->column('type', '显示地点')->using([
                1 => '首页',
                2 => '分类页面',
                3 => '兑换专区',
            ])->sortable();
            $grid->column('is_active')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Carousel(), function (Show $show) {
            $show->field('id');
            $show->field('product_id');
            $show->field('image_path');
            $show->field('title');
            $show->field('description');
            $show->field('link');
            $show->field('order');
            $show->field('is_active');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Carousel(), function (Form $form) {
            $form->display('id');
            $form->select('product_id', '关联产品')->options($this->getProduct());
            $form->image('image_path')->autoUpload();
            $form->text('title');
            $form->select('type', '显示地点')->options([
                1 => '首页',
                2 => '分类页面',
                3 => '兑换专区',
            ])->default(1);
            $form->text('description');
            $form->url('link');
            $form->number('order');
            $form->switch('is_active')->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
